package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    TextView textView;
    TextView textViewoutputtemp;
    TextView textViewoutputwind;
    TextView textViewoutputshidu;
    TextView textViewoutputcity;
    TextView textViewtemp;
    TextView textViewwind;
    TextView textViewcity;
    TextView textViewshidu;
    EditText editText;
    ProgressBar progressBar;
    ImageView imageView;
    private String cityName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=findViewById(R.id.txt_download);
        editText=findViewById(R.id.txt_input);
        progressBar = findViewById(R.id.progressBar);
        textViewoutputcity=findViewById(R.id.txt_output_city);
        textViewoutputwind=findViewById(R.id.txt_output_wind);
        textViewoutputshidu=findViewById(R.id.txt_output_shidu);
        textViewoutputtemp=findViewById(R.id.txt_output_temp);
        textViewtemp=findViewById(R.id.txt_temp);
        textViewwind=findViewById(R.id.txt_wind);
        textViewcity=findViewById(R.id.txt_city);
        textViewshidu=findViewById(R.id.txt_shidu);
        imageView=findViewById(R.id.img_wether);
    }

    public void download(View view) {
        try {
            AsyncTask<URL,Integer,String> asyncTask=new MyDownladTask();
            asyncTask.execute(new URL("http://www.baidu.com"));
        }catch (MalformedURLException e){
            e.printStackTrace();
        }
    }

    public void search(View view) {
        String[] cityCodeArray = getResources().getStringArray(R.array.cityCode);
        String[] cityNameArray = getResources().getStringArray(R.array.cityName);
        List<String> cityNameList = Arrays.asList(cityNameArray);
        int index = cityNameList.indexOf(editText.getText().toString());
        Log.d("MainActivity","index="+index);
        if (index == -1) {
            Toast.makeText(MainActivity.this, "输入城市名称未录入系统,请重新输入", Toast.LENGTH_SHORT).show();
            return;
        }
        Log.d("MainActivity","etCityCode"+editText.getText().toString());
        WeatherTask weatherTask=new WeatherTask();
        try {
            weatherTask.execute(new URL("https://api.help.bj.cn/apis/weather/?id="+cityCodeArray[index]));
        }catch (MalformedURLException e){
            e.printStackTrace();
        }
    }
    class WeatherTask extends AsyncTask<URL,Integer,String>{
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            WeatherBean weatherBean= JSON.parseObject(s, WeatherBean.class);
            textViewoutputtemp.setText(weatherBean.getTemp());
            textViewoutputshidu.setText(weatherBean.getTempf());
            textViewoutputcity.setText(weatherBean.getCity());
            textViewoutputwind.setText(weatherBean.getWd());
            
            Log.d("MainActivity","获得天气数据,温度:"+weatherBean.getTemp());
        }

        protected String doInBackground(URL... urls) {
            String s=HttpGetData(urls[0]);

            Log.d("MainActivity","获得天气数据"+s);
            return s;
        }
    }
    class MyDownladTask extends AsyncTask<URL,Integer,String>
    {
        protected void onPreExecute(){
            super.onPreExecute();
            textView.setText("开始下载");
        }
        protected void onPostExecute(String s){
            super.onPostExecute(s);
            textView.setText("下载成功");
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            progressBar.setProgress(values[0]);
        }

        @Override
        protected String doInBackground(URL... urls) {
            Log.d("MainActivity","download:开始");

            //体验5秒
           // try {
           //     for (int i = 0; i < 100; i++) {
            //        Thread.sleep(50);
             //       publishProgress(i);
            //    }

         //   }catch (InterruptedException e){
          //      e.printStackTrace();
           // }
            Log.d("MainActivity","download:完成");
            return null;
        }
    }
    public  String HttpGetData(URL url){
        String s = null;
        try {
            HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
            //发起连接
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.connect();
            //请求教程
            if (httpURLConnection.getResponseCode()==200){
                InputStream inputStream=httpURLConnection.getInputStream();
                BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
                s = bufferedReader.readLine();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
       //网络连接创建
        return  s;
    }
}